﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmReports_SVTrongGV : FormBase
    {
        public frmReports_SVTrongGV()
        {
            InitializeComponent();
        }

        private void frmReports_SVTrongGV_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGiangVien();
                cboGiangVien.ItemIndex = 0;
                cboNamThucTap.ItemIndex = 0;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboGiangVien.EditValue != null && cboNamThucTap.EditValue != null)
                {
                    int giangvienID = Convert.ToInt32(cboGiangVien.EditValue);
                    int namID = Convert.ToInt32(cboNamThucTap.EditValue);
                    var query = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.GiangVienTrongNam.NamThucTap.Oid == namID).Select(c => new
                    {
                        c.Oid,
                        SinhVienID = c.SinhVien.Oid,
                        c.SinhVien.MaSinhVien,
                        c.SinhVien.HoSinhVien,
                        c.SinhVien.TenSinhVien
                    });
                    Reports.dsReport ds = new Reports.dsReport();
                    foreach (var item in query)
                    {
                        DataRow dr = ds.SVTheoGV.NewRow();
                        dr["MaSV"] = item.MaSinhVien;
                        dr["HoTenSV"] = item.HoSinhVien + " " + item.TenSinhVien;
                        var objCongTy = Session.DefaultSession.Query<SinhVienTrongCongTy>().Where(c => c.CongTyTrongNam.NamThucTap.Oid == namID && c.SinhVien.Oid == item.SinhVienID).Select(c => new { c.CongTyTrongNam.CongTy.Oid, c.CongTyTrongNam.CongTy.TenCongTy }).FirstOrDefault();
                        if (objCongTy != null)
                        {
                            dr["TenCongTy"] = objCongTy.TenCongTy;
                            var objDiaChi = Session.DefaultSession.Query<DiaChi>().FirstOrDefault(c => c.CongTy.Oid == objCongTy.Oid);
                            if (objDiaChi != null)
                            {
                                dr["DiaChi"] = objDiaChi.ToaNha + " " + objDiaChi.SoNha + " " + objDiaChi.TenDuong + ", " + objDiaChi.PhuongXa +
                                    ", " + objDiaChi.QuanHuyen.TenQuanHuyen + ", " + objDiaChi.TinhThanh.TenTinhThanh;
                            }
                            var objLienHe = Session.DefaultSession.Query<LienHe>().FirstOrDefault(c => c.CongTy.Oid == objCongTy.Oid);
                            if (objLienHe != null)
                            {
                                dr["DienThoai"] = objLienHe.DienThoai;
                            }
                        }
                        ds.SVTheoGV.Rows.Add(dr);
                    }
                    Reports.rpSVTheoGV rp = new Reports.rpSVTheoGV();
                    var objGiangVien = Session.DefaultSession.Query<GiangVien>().FirstOrDefault(c => c.Oid == giangvienID);
                    if (objGiangVien != null)
                    {
                        rp.Parameters["giangvien"].Value = "DANH SÁCH SINH VIÊN THỰC TẬP THEO GIÁO VIÊN HƯỚNG DẪN: " + objGiangVien.HoGiangVien.ToUpper() + " " + objGiangVien.TenGiangVien.ToUpper();
                        rp.Parameters["giangvien"].Visible = false;
                    }
                    rp.DataSource = ds;
                    DevExpress.XtraReports.UI.ReportPrintTool printTool = new DevExpress.XtraReports.UI.ReportPrintTool(rp);
                    printTool.ShowPreviewDialog();
                }
                else
                    CommonMessage("Chọn năm thực tập và chọn ngành");
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadGiangVien()
        {
            int namThucTap = Convert.ToInt32(cboNamThucTap.EditValue);
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (var item in Session.DefaultSession.Query<GiangVien>().Where(c => c.GiangVienTrongNams.Any(d => d.NamThucTap.Oid == namThucTap)))
            {
                dic.Add(item.Oid, item.HoGiangVien + " " + item.TenGiangVien);
            }
            cboGiangVien.Properties.DataSource = dic;
        }

        private void cboNamThucTap_EditValueChanged(object sender, EventArgs e)
        {
            LoadGiangVien();
        }
    }
}